package org.fusionide.builder.parsers.cfml.tags;

import java.util.Vector;

import org.fusionide.builder.parsers.IParserHandler;
import org.fusionide.builder.parsers.ParserException;
import org.fusionide.builder.parsers.cfml.CFMLTag;
import org.fusionide.builder.parsers.cfml.Variable;

public class Loop extends CFMLTag {

	public String collection;

	public String condition;

	public String delimiters;

	public String endrow;

	public String from;

	public String index;

	public String item;

	public String list;

	public String query;

	public String startrow;

	public String step;

	public String to;

	public Loop(String attributes, int startOffset) {
		super(attributes, startOffset);

		if (attributes == null)
			return;

		this.collection = getAttribute("collection");
		this.condition = getAttribute("condition");
		this.delimiters = getAttribute("delimiters");
		this.endrow = getAttribute("endrow");
		this.from = getAttribute("from");
		this.index = getAttribute("index");
		this.item = getAttribute("item");
		this.list = getAttribute("list");
		this.query = getAttribute("query");
		this.startrow = getAttribute("startrow");
		this.step = getAttribute("step");
		this.to = getAttribute("to");
	}

	@Override
	public boolean requiresClosingTag() {
		return true;
	}

	@Override
	public Vector<Variable> getVariables(int offset) {
		Vector<Variable> vars = new Vector<Variable>();

		if (offset <= startOffset)
			return vars;

		if (index != null) {
			vars.add(new Variable(index, startLine, false));
		} else if (item != null) {
			vars.add(new Variable(item, startLine, false));
		}
		vars.addAll(super.getChildVariables(offset));

		return vars;
	}

	@Override
	public boolean validate(IParserHandler parserHandler) {
		if (super.validate(parserHandler)) {
			if (condition != null && !condition.equalsIgnoreCase("")) {
				return true;
			}
			if (query != null && !query.equalsIgnoreCase("")) {
				return true;
			}

			if (collection != null && item != null && !collection.equalsIgnoreCase("") && !item.equalsIgnoreCase(""))
				return true;

			if (index != null && list != null && !index.equalsIgnoreCase("") && !list.equalsIgnoreCase("")) {
				return true;
			}
			if (index != null && from != null && to != null && !index.equalsIgnoreCase("") && !from.equalsIgnoreCase("") && !to.equalsIgnoreCase("")) {
				return true;
			}
			if (parserHandler != null)
				parserHandler.addMarker(new ParserException("<cfloop> Missing required attributes", ParserException.SEVERITY_ERROR, startLine));
			return true;
		} else
			return false;
	}

	public String getCollection() {
		return collection;
	}

	public void setCollection(String collection) {
		this.collection = collection;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String getDelimiters() {
		return delimiters;
	}

	public void setDelimiters(String delimiters) {
		this.delimiters = delimiters;
	}

	public String getEndrow() {
		return endrow;
	}

	public void setEndrow(String endrow) {
		this.endrow = endrow;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public String getItem() {
		return item;
	}

	public void setItem(String item) {
		this.item = item;
	}

	public String getList() {
		return list;
	}

	public void setList(String list) {
		this.list = list;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public String getStartrow() {
		return startrow;
	}

	public void setStartrow(String startrow) {
		this.startrow = startrow;
	}

	public String getStep() {
		return step;
	}

	public void setStep(String step) {
		this.step = step;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

}
